
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:reabampos/src/reabam/operation/db/objectbox.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui/support/support_get_good_items.dart';
import 'package:reabampos/src/reabam/ui_self_service/mine/pagestate_ss_setting.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_login.dart';
import 'package:reabampos/src/reabam/utils/brean/c_bean_good_item.dart';
import 'package:reabampos/src/reabam/utils/rb_business.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
手动添加购物袋
 */
class PageStateSelfServiceManualAddCartRelatedItem extends XPageState{

  String itemId;
  String title;
  List<ComponentBeanGoodItem> list_CartRelatedItem=[];


  PageStateSelfServiceManualAddCartRelatedItem(
      this.title,
      this.itemId,
      );


  @override
  void onCreate() {
    super.onCreate();
    RBConfigs.isPageScanType=0;
  }

  @override
  Widget getBodyWidget(BuildContext context) {
    return XWidgetUtils.getLayoutCenter(
        XWidgetUtils.getXLayoutRoundedRect(
            height:1350,
            radius_topLeft: 30,
            radius_topRight: 30,
            radius_bottomRight: 30,
            radius_bottomLeft: 30,
            margin: XWidgetUtils.getEdgeInsets(50, 0, 50, 0),
            backgroundColor: Colors.white,
            child:XWidgetUtils.getLayoutColumn(
                children: [
                  XWidgetUtils.getLayoutContainer(
                      width: double.infinity,
                      margin: XWidgetUtils.getEdgeInsets(0, 60, 0, 0),
                      child:XWidgetUtils.getLayoutStack(
                          alignment:AlignmentDirectional.center,
                          children: [
                            XWidgetUtils.getWidgetText(
                                title,
                                style: XStyleUtils.getTextStyle(
                                  fontSize: 46,
                                  color: XColorUtils.color333333,
                                )
                            ),
                            XWidgetUtils.getLayoutPositioned(
                                top: 0,
                                right: 32,
                                child: XWidgetUtils.getWidgetGestureDetector(
                                    child: XImageUtils.getImageWidgetWithAsset(
                                        'ss_close.png',
                                        width: 70,
                                        height: 70
                                    ),
                                    onTap: (){
                                      finishPage();
                                    }
                                )
                            )
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutExpanded(
                      XWidgetUtils.getLayoutContainer(
                          margin: XWidgetUtils.getEdgeInsets(20, 34, 20, 0),
                          child: XWidgetUtils.getWidgetGridView(
                              crossAxisCount: 3,
                              listData: list_CartRelatedItem,
                              childHeight: 510,
                              listItemWidgetFunction: (BuildContext context, int index){
                                ComponentBeanGoodItem item =list_CartRelatedItem[index];

                                return XWidgetUtils.getLayoutContainer(
                                  margin: XWidgetUtils.getEdgeInsets(0, 20, 0, 20),
                                  alignment: Alignment.center,
                                  child: XWidgetUtils.getLayoutColumn(
                                      shrinkWrap:true,
                                      mainAxisAlignment:MainAxisAlignment.center,
                                      crossAxisAlignment :CrossAxisAlignment.center,
                                      children: [
                                        XImageUtils.getImageWidgetWithUrl(
                                            width: 200,
                                            height: 200,
                                            imageUrl: item.imageUrlFull??'',
                                            defaultImageAssetName: 'ss_defalut_cartrelateditem.png'
                                        ),
                                        XWidgetUtils.getWidgetText(
                                          item.itemName??'',
                                          maxLines: 1,
                                          style: XStyleUtils.getTextStyle(
                                              fontSize: 34,
                                              color: XColorUtils.color333333
                                          ),
                                          padding: XWidgetUtils.getEdgeInsets(0, 24, 0, 0),
                                        ),
                                        XStringUtils.isEmpty(item.specName)?XWidgetUtils.getLayoutContainer(): XWidgetUtils.getWidgetText(
                                          item.specName??'',
                                          maxLines: 1,
                                          style: XStyleUtils.getTextStyle(
                                              fontSize: 30,
                                              color: XColorUtils.color333333
                                          ),
                                          padding: XWidgetUtils.getEdgeInsets(0, 10, 0, 0),
                                        ),
                                        XWidgetUtils.getWidgetText(
                                          '¥${XNumberUtils.formatDoubleX(item.currentPrice,n: 2)}',
                                          style: XStyleUtils.getTextStyle(
                                              fontSize: 30,
                                              color: XColorUtils.color333333
                                          ),
                                          padding: XWidgetUtils.getEdgeInsets(0, 10, 0, 0),
                                        ),
                                        RBWidgets_SelfService.getCountChangeWidget(
                                            margin: XWidgetUtils.getEdgeInsets(0, 16, 0, 7),
                                            text: XNumberUtils.formatDoubleX(item.currentQuantity),
                                            onClick: (btN) {
                                              if(btN=='-'){

                                                setState(() {
                                                  if(item.currentQuantity<=1){
                                                    item.currentQuantity=0;
                                                  }else{
                                                    item.currentQuantity=XNumberUtils.sub(item.currentQuantity, 1);
                                                  }
                                                });

                                              }else if(btN=='+'){
                                                //
                                                setState(() {
                                                  item.currentQuantity=XNumberUtils.add(item.currentQuantity, 1);
                                                });
                                              }
                                            }
                                        ),
                                      ]
                                  ),
                                );
                              }
                          )
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(70, 45, 70, 60),
                      child: XWidgetUtils.getLayoutRow(
                          children: [
                            XWidgetUtils.getLayoutExpanded(XWidgetUtils.getWidgetGestureDetector(
                                child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                    radius_topLeft: 16,
                                    radius_topRight: 16,
                                    radius_bottomRight: 16,
                                    radius_bottomLeft: 16,
                                    margin: XWidgetUtils.getEdgeInsets(10, 0, 10, 0),
                                    height: 120,
                                    borderColor: RBColors.reabamColor4,
                                    backgroundColor: Colors.white,
                                    alignment: Alignment.center,
                                    child: XWidgetUtils.getWidgetText(
                                        '不需要',
                                        style: XStyleUtils.getTextStyle(
                                          fontSize: 46,
                                          color: RBColors.reabamColor4,
                                        )
                                    )
                                ),
                                onTap: (){
                                  finishPage();
                                }
                            )),
                            XWidgetUtils.getLayoutExpanded(XWidgetUtils.getWidgetGestureDetector(
                                child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                    radius_topLeft: 16,
                                    radius_topRight: 16,
                                    radius_bottomRight: 16,
                                    radius_bottomLeft: 16,
                                    margin: XWidgetUtils.getEdgeInsets(10, 0, 10, 0),
                                    height: 120,
                                    borderColor: RBColors.reabamColor4,
                                    backgroundColor: RBColors.reabamColor4,
                                    alignment: Alignment.center,
                                    child: XWidgetUtils.getWidgetText(
                                        '确认',
                                        style: XStyleUtils.getTextStyle(
                                          fontSize: 46,
                                          color: Colors.white,
                                        )
                                    )
                                ),
                                onTap: (){
                                  bool isAdd=false;

                                  for(ComponentBeanGoodItem iitem in list_CartRelatedItem){
                                    if(iitem.currentQuantity>0){
                                      isAdd=true;
                                      break;
                                    }
                                  }

                                  if(isAdd){
                                    finishPage(result: list_CartRelatedItem);
                                  }else{
                                    toast('请添加商品');
                                  }

                                }
                            )),

                          ]
                      )
                  )
                ]
            )
        )
    );
  }



  @override
  Color getXPageBackgroundColor() {
    return XColorUtils.pageMengcengColor;
  }


  @override
  void onStart() {
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {

    Future((){
      //1.查找商品
      Map<String,dynamic> requestMap={};
      requestMap['pageIndex']=1;
      requestMap['pageSize']=20;
      requestMap['sword']=itemId;

      //
      SupportGetGoodItems.searchGoodItems(
          this,
          requestMap,
          isSearchMainItem:false,
          callback:(Map<String,dynamic> result) {

            List<Map<String, dynamic>> items=result['items']??[];
            int size=items.length;
            //XLogUtils.printLog('---搜索数据库结果items,size=$size');
            if(size!=1){
              toast('条码对应商品不存在，请联系店员');
              return;
            }
            //XLogUtils.printLog('---items[0]=${XJsonUtils.obj2Json(items[0])}',isPrintAll: true);
            //XFileUtils.writeToFile('${XTempData.appDocumentsDirectoryPath}/findItemJson.txt', XJsonUtils.obj2Json(items[0]));

            //添加到购物车
            ComponentBeanGoodItem item =ComponentBeanGoodItem.fromJson(items[0]);

            if(RBBusinessUtils.isSpecType(item)){
              //多规格
              XObjectBoxUtils.getSpecItem(
                  item.itemId??'',
                      (Map<String,dynamic> result){
                    List<dynamic> specItems=result['items']??[];

                    for(dynamic specItem in specItems){
                      ComponentBeanGoodItem itemS =ComponentBeanGoodItem.fromJson(specItem);
                      itemS.itemName=item.itemName;
                      itemS.staff=XJsonUtils.json2Map(XSharePreferencesUtils.getString(RBConfigs.SP_staff));
                      itemS.currentPrice=itemS.dealPrice??0;
                      itemS.currentQuantity=0;
                      itemS.currentMoney=XNumberUtils.mul(itemS.currentQuantity, itemS.currentPrice);
                      list_CartRelatedItem.add(itemS);
                    }

                    setState(() {
                    });

                  }
              );

            }else{
              //单规格
              item.id='${XDateUtils.getCurrentTime_ms()}${XNumberUtils.randomInt(100)+900}';
              item.staff=XJsonUtils.json2Map(XSharePreferencesUtils.getString(RBConfigs.SP_staff));
              item.currentPrice=item.dealPrice??0;
              item.currentQuantity=0;
              item.currentMoney=XNumberUtils.mul(item.currentQuantity, item.currentPrice);
              list_CartRelatedItem.add(item);

              setState(() {
              });

            }

          }
      );
    });


  }

}